#pragma once

#include <string>
#include "../models/recordmodel.hpp"
#include "../views/recordview.hpp"
#include "../util.hpp"

namespace pharmacy_server
{
    class RecordController
    {
    public:
        static RecordController& instance()
        {
            static RecordController inst;
            return inst;
        }

    private:
        RecordController()
            : recordmodel(SalesRecordModel::instance())
        {}
        RecordController(const RecordController&) = delete;
        RecordController& operator=(const RecordController&) = delete;

    public:
        Result query_sales_records(const std::string& medicine_name, const std::string& username, const std::string& start_date, const std::string& end_date)
        {
            try
            {
                auto records = recordmodel.query_sales_records(medicine_name, username, start_date, end_date);

                Json::Value root;
                for (auto* record : records)
                    root.append(record->to_json());

                return recordview.query_sales_records(true, "query sales records success", json_util::serialize(root));
            }
            catch (const model_except& e)
            {
                return recordview.query_sales_records(false, e.what());
            }
        }

    private:
        SalesRecordModel& recordmodel;
        RecordView recordview;
    };
}